﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.LotterySrv;
using SLLottery.Common;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace SLLottery
{
    public partial class LowerList : UserControl
    {
        public LotterySrv.LotteryServiceClient srv_UserList;
        User user = (User)App.Session[Constant.USERINFO_SESSION];
        ObservableCollection<LowerListData> obLowerData;
        LowerListData PageSum;
        LowerListData TotalSum;
        List<long> BindedLst;
        private int currentPos = 0;
        public LowerList()
        {
            InitializeComponent();
            PageSum = new LowerListData();
            PageSum.UserID = "本页合计";
            PageSum.IsOnline = -1;
            TotalSum = new LowerListData();
            TotalSum.UserID = "总合计";
            TotalSum.IsOnline = -1;
            BindedLst = new List<long>();
            srv_UserList = WcfProxy.GetProxy;
            srv_UserList.GetLowerListDataCompleted += new EventHandler<LotterySrv.GetLowerListDataCompletedEventArgs>(srv_UserList_GetLowerListDataCompleted);
            srv_UserList.GetLowerListDataByUIDCompleted += new EventHandler<GetLowerListDataByUIDCompletedEventArgs>(srv_UserList_GetLowerListDataByUIDCompleted);
            BindLowerList(user.ID);
            TextBlock txtb = new TextBlock()
            {
                Text = "本级",
                Style = Resources["TextBlockStyle1"] as Style,
                Cursor=Cursors.Hand
            };
            if (user.RoleID == 3)
                btAddLower.Visibility = Visibility.Collapsed;
            txtb.Tag = user.ID;
            BindedLst.Add(user.ID);
            txtb.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_MouseLeftButtonDown);
            NavMenu.Children.Add(txtb);
        }

        void srv_UserList_GetLowerListDataByUIDCompleted(object sender, GetLowerListDataByUIDCompletedEventArgs e)
        {
            if (e.Result.Count != 0)
            {
                obLowerData = e.Result as ObservableCollection<LowerListData>;
                PageSum.GameScores = obLowerData[0].GameScores;
                PageSum.EnableScores = obLowerData[0].EnableScores;
                PageSum.DisableScores = obLowerData[0].DisableScores;
                TotalSum.GameScores = obLowerData[0].GameScores;
                TotalSum.EnableScores = obLowerData[0].EnableScores;
                TotalSum.DisableScores = obLowerData[0].DisableScores;
                obLowerData.Add(PageSum);
                obLowerData.Add(TotalSum);
                currentPos = 0;
                PagedCollectionView pageView = new PagedCollectionView(obLowerData);
                pageView.PageSize = 20;
                dpList.PageSize = 20;
                dpList.Source = pageView;
                dgList.ItemsSource = pageView;
            }
            gridLoading.Visibility = Visibility.Collapsed;
        }

        private void BindLowerList(Int64 id)
        {
            srv_UserList.GetLowerListDataAsync(id);
            gridLoading.Visibility = Visibility.Visible;
        }
        void srv_UserList_GetLowerListDataCompleted(object sender, LotterySrv.GetLowerListDataCompletedEventArgs e)
        {
            if (e.Result.Count!=0)
            {
                obLowerData = e.Result as ObservableCollection<LowerListData>;
                PageSum.GameScores = obLowerData.Take(18).Sum(p => p.GameScores);
                PageSum.EnableScores = obLowerData.Take(18).Sum(p => p.EnableScores);
                PageSum.DisableScores = obLowerData.Take(18).Sum(p => p.DisableScores);
                TotalSum.GameScores = obLowerData.Sum(p => p.GameScores);
                TotalSum.EnableScores = obLowerData.Sum(p => p.EnableScores);
                TotalSum.DisableScores = obLowerData.Sum(p => p.DisableScores);
                obLowerData.Add(PageSum);
                obLowerData.Add(TotalSum);
                currentPos = 0;
                PagedCollectionView pageView = new PagedCollectionView(obLowerData);
                pageView.PageSize = 20;
                dpList.PageSize = 20;
                dpList.Source = pageView;
                dgList.ItemsSource = pageView;
            }
            gridLoading.Visibility = Visibility.Collapsed;
        }

        private void LowerPlayListUserName_Click(object sender, MouseButtonEventArgs e)
        {
            LowerListData info = (sender as TextBlock).DataContext as LowerListData;
            if (!BindedLst.Contains(info.ID))
            {
              if (((info != null) && (info.RoleName == "代理")))
              {
                  BindedLst.Add(info.ID);
                  TextBlock txtb = new TextBlock()
                  {
                      Text=">",
                      Style = Resources["TextBlockStyle2"] as Style,
                  };
                  NavMenu.Children.Add(txtb);
                  txtb = new TextBlock()
                  {
                      Text=info.UserID,
                      Style = Resources["TextBlockStyle0"] as Style,
                  };
                  txtb.Tag = info.ID;
                  txtb.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_MouseLeftButtonDown);
                  NavMenu.Children.Add(txtb);
                  BindLowerList(info.ID);
              }
            }
        }

        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (obLowerData[e.Row.GetIndex()].IsOnline != -1)
            {
                currentPos += 1;
                (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = currentPos.ToString();
                (this.dgList.Columns[1].GetCellContent(e.Row) as TextBlock).Tag = e.Row.DataContext;
            }
            else
            {
                this.dgList.Columns[10].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[7].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[3].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
            }
            LowerListData ld = e.Row.DataContext as LowerListData;
            if(ld.RoleName=="会员")
                (dgList.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Common.MyColor.ConvertColor("#ff2ADC16"));
            if (ld.UserID == user.UserId)
                this.dgList.Columns[10].GetCellContent(e.Row).Visibility = Visibility.Collapsed;
            //if(e.Row.GetIndex()==0)
            //{
            //    StackPanel sp = this.dgList.Columns[3].GetCellContent(e.Row) as StackPanel;
            //    Image image = sp.Children[1] as Image;
            //    image.Visibility = Visibility.Collapsed;
            //}
        }

        private void btAddLower_Click(object sender, RoutedEventArgs e)
        {
            LowerWindow lw = new LowerWindow();
            lw.Closed += (s, er) =>
            {
                if ((bool)lw.DialogResult == true)
                {
                    btRefreshList_Click(null, null);
                }
            };
            lw.ShowAddWindow(user.Bonus);
           
        }

        private void btSearchList_Click(object sender, RoutedEventArgs e)
        {
            SearchUser searchUser = new SearchUser();
            searchUser.Closed += new EventHandler(SearchUser_Close);  //在父窗体注册子窗体的关闭事件
            searchUser.Show();
        }
        private void SearchUser_Close(object sender, EventArgs e)
        {
            SearchUser searchUser = sender as SearchUser;
            bool? result = searchUser.DialogResult;

            if (result.HasValue && result.Value)
            {
                srv_UserList.GetUserInfoAsync(searchUser.userID);
                srv_UserList.GetUserInfoCompleted += (s, er) =>
                {
                    if (er.Result == null)
                        return;
                    if (!BindedLst.Contains(er.Result.ID))
                    {
                        TextBlock txtb = new TextBlock()
                        {
                            Text = ">",
                            Style = Resources["TextBlockStyle2"] as Style,
                        };
                        NavMenu.Children.Add(txtb);
                        txtb = new TextBlock()
                        {
                            Text = er.Result.UserId,
                            Style = Resources["TextBlockStyle0"] as Style,
                        };
                        txtb.Tag = er.Result.ID;
                        txtb.MouseLeftButtonDown += new MouseButtonEventHandler(TextBlock_MouseLeftButtonDown);
                        NavMenu.Children.Add(txtb);
                        BindedLst.Add(er.Result.ID);
                    }
                    srv_UserList.GetLowerListDataAsync(er.Result.ID);
                };
            }
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock txtb=sender as TextBlock;
            int index = NavMenu.Children.IndexOf(txtb);
            if (index!=NavMenu.Children.Count-1)
            {
                for (int i = NavMenu.Children.Count - 1; i > index; i--)
                {
                    TextBlock txtblock = NavMenu.Children[i] as TextBlock;
                    if(txtblock.Text!=">")
                        BindedLst.Remove(Int64.Parse(txtblock.Tag.ToString()));
                    NavMenu.Children.RemoveAt(i);
                }
            }
            BindLowerList(Int64.Parse(txtb.Tag.ToString())); 
        }

        private void btRefreshList_Click(object sender, RoutedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Visible;
            BindLowerList(user.ID);
        }

        private void EditLower_Click(object sender, RoutedEventArgs e)
        {
            LowerWindow lw = new LowerWindow();
            lw.Closed += (s, er) =>
            {
                if ((bool)lw.DialogResult == true)
                {
                    btRefreshList_Click(null, null);
                }
            };
            Button bt = sender as Button;
            lw.ShowEditWindow(Convert.ToInt64(bt.Tag.ToString()),user.Bonus);
        }
    }
}
